package edu.sjsu.cs.queryopt.dbms.main;

import java.util.ArrayList;
import java.util.Hashtable;

public class InList {
	
	/**
	 * SELECT T1.5 FROM T1 WHERE T1.1 IN ( 5 ) AND T1.4 = 10
	 * SELECT T1.5 FROM T1 WHERE T1.1 IN ( 5, 20, 50 ) AND T1.5 = 50
	 * SELECT T1.5 FROM T1 WHERE T1.1 IN ( 5, 20, 50 ) OR T1.5 = 50
	 * SELECT T1.5 FROM T1 WHERE T1.1 IN ( 5 ) OR T1.4 = 10
	 * 
	 * memorizing the psoitions
	 * */
	
	public  Hashtable<String, ArrayList<Integer>> mapping (ArrayList<String> conditionpod)
	{
		Hashtable<String, ArrayList<Integer>>  map = new Hashtable<String, ArrayList<Integer>>();
		ArrayList<Integer> position = new ArrayList<Integer>();
		
		for(int first = 0 ; first <  conditionpod.size() ; first++){
			if(conditionpod.get(first).trim().equals("IN")){
				position.add(first);
			}
		}
		map.put("IN", position);
		position = new ArrayList<Integer>();
		return null;
	}
	  
	 
	
	public Hashtable<String, ArrayList<String>>getALLPedicate(ArrayList<String> conditionpod ){
		
		ArrayList<String> liand = new ArrayList<String>();
		ArrayList<String> lior = new ArrayList<String>();
		ArrayList<String> liin = new ArrayList<String>();
		ArrayList<String> dummy  = new ArrayList<String>();
		Hashtable<String, ArrayList<String>> ht = new Hashtable<String, ArrayList<String>>();
		int index = 0;
		while(index < conditionpod.size()){
			
			if(conditionpod.get(index).trim().startsWith("T")){
				dummy.add(conditionpod.get(index));
			}	
			if(conditionpod.get(index).trim().equals("AND")){
				liand.addAll(dummy);
				dummy  = new ArrayList<String>();
			
			}
			else if(conditionpod.get(index).trim().equals("OR")){
				lior.addAll(dummy);
				dummy  = new ArrayList<String>();
				
			}
			else if(conditionpod.get(index).trim().equals("IN")){
				liin.addAll(dummy);
				dummy  = new ArrayList<String>();
			}
			index++;
			
		}
//		System.out.println("DUM DUM "+ dummy);
		/**
		 * we assume we did put some on the last in the query
		 * */
		if(dummy.size() > 0){
//			System.out.println("get(conditionpod.size()-2)"  + conditionpod.get(conditionpod.size()-4));
			if(conditionpod.get(conditionpod.size()-4).trim().equals("AND")){
				liand.addAll(dummy);
				dummy  = new ArrayList<String>();
			}
			else if(conditionpod.get(conditionpod.size()-4).trim().equals("OR")){
				lior.addAll(dummy);
				dummy  = new ArrayList<String>();
			}
			else if(conditionpod.get(conditionpod.size()-4).trim().equals(")")){
				liin.addAll(dummy);
				dummy  = new ArrayList<String>();
			}
			
		}
		ht.put("AND", liand);
		ht.put("OR", lior);
		ht.put("IN", liin);
		
//		System.out.println("ht BOB AND part " + ht);
//		System.out.println("ht BOB3 " + ht);
		return ht;
		
	}
 
}
